//
//  JCMessageTextContent.swift
//  JChat
//
//  Created by JIGUANG on 2017/3/9.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory
import DeviceKit

open class JCMessageTextContent: NSObject, JCMessageContentType {
    public weak var delegate: JCMessageDelegate?
    public override init() {
        let text = "this is a test text"
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(text: String) {
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(attributedText: NSAttributedString) {
//        self.text = attributedText
        
        
        
        let attribute: NSMutableAttributedString = NSMutableAttributedString(string: attributedText.string)
        
        let paraStyle = NSMutableParagraphStyle()
        
        paraStyle.alignment = .left
        
        attribute.addAttributes([NSAttributedString.Key.font : PingFangSCRegularFont(14),
                                 NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a,
                                  NSAttributedString.Key.paragraphStyle : paraStyle],
                                 range: NSRange(location: 0, length: attributedText.string.count))
        
        self.text = attribute
        
        
        super.init()
    }
    
    open class var viewType: JCMessageContentViewType.Type {
        return JCMessageTextContentView.self
    }
    open var layoutMargins: UIEdgeInsets = .init(top: 8, left: 5, bottom: 8, right: 5)
    
    open var text: NSAttributedString
    
    open func sizeThatFits(_ size: CGSize) -> CGSize {
        let attr = NSMutableAttributedString(attributedString: self.text)
        
        attr.addAttributes([NSAttributedString.Key.font : PingFangSCRegularFont(14),
                                  NSAttributedString.Key.foregroundColor : UIColor.white],
                                range: NSRange(location: 0, length: self.text.length))
        
        
//        let mattrSize = attr.boundingRect(with: CGSize(width: 200.0, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
//
//        if mattrSize.height <= 21 {
//            let size = CGSize(width: mattrSize.size.width < 30 ? 28 : mattrSize.size.width, height: mattrSize.size.height + 4)
//            return size
//        }
//
//        let size = CGSize(width: mattrSize.size.width + 20, height: mattrSize.size.height + 8)
//        return size
        var width: CGFloat = 250.0
        switch Device.current {
        case .iPhone4, .iPhone5, .iPhone5s, .iPhone6:
            width = 200
            break
        case .iPhone7, .iPhoneSE, .iPhone8:
            width = 210
            break
        case .iPhone6Plus, .iPhone6sPlus, .iPhone7Plus, .iPhone8Plus, .iPhoneX:
            width = 250
            break
        default:
            width = 250
            break
        }
        
        let layout: YYTextLayout = YYTextLayout(containerSize: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude) , text: attr)!
        
        return CGSize(width: layout.textBoundingSize.width, height: layout.textBoundingSize.height + 4)
    }
}
